package com.ssb.admin.common.aspect;

import com.ssb.admin.common.exception.ApiException;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Value;

@Slf4j
@Aspect
public class RedisAspect {
    @Value("${ssbadmin.redis.open: false}")
    private boolean open;

    @Around("execution(* com.ssb.admin.common.utils.RedisUtils.*(..))")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        // redis开启先查询redis 否则查询数据库
        Object result = null;
        if(open){
            try{
                result = point.proceed();
            }catch (Exception e){
                log.error("redis error", e);
                throw new ApiException("Redis服务异常");
            }
        }
        // 将查询到的数据返回 查询数据库后把数据放入redis
        return result;
    }
}
